package com.github.McPlugin.event;

import com.github.McPlugin.main;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.EquipmentSlot;

public class PlayersEvent implements Listener {

    @EventHandler
    public void PlayerJoin(PlayerJoinEvent event) {
        main.plugin.getLogger().info("触发了进入服务器事件");
        Player player = event.getPlayer();
//        new BukkitRunnable(){
//
//            @Override
//            public void run() {
//                if(!MySQLManager.get().hasPlayer(player.getUniqueId().toString()))
//                {
//                    MySQLManager.get().insertPlayer(player.getUniqueId().toString(),player.getName());
//                }
//            }
//        }.runTaskAsynchronously(main.plugin);

        //event.getPlayer().sendMessage("§4欢迎至尊VIP归来： 【" + event.getPlayer().getName() + "】");

        event.getPlayer().teleport(new Location(event.getPlayer().getWorld(), 140, 107, 588));
        //event.getPlayer().

        int count = main.plugin.getConfig().getInt(event.getPlayer().getName());
        //if(count == 0){
        count++;
        main.plugin.getConfig().set(event.getPlayer().getName(), count);
        main.plugin.saveConfig();
        //}
        event.getPlayer().sendMessage("你是第" + count + "次进入服务器");
//        ItemStack itemStack = new ItemStack(Material.BOOK);
//        ItemMeta itemMeta = itemStack.getItemMeta();
//        itemMeta.setLore(Arrays.asList("First ", "second"));
//        itemStack.setItemMeta(itemMeta);
//        player.getInventory().setItem(0, itemStack);
//        player.getInventory().getItem(0).set




    }


    @EventHandler
    public void PlayerClick(PlayerInteractEvent event) {
        //main.plugin.getLogger().info("触发了点击事件");
        if (event.getHand() == EquipmentSlot.HAND) {
            //event.getPlayer().sendMessage("手中物品数量加一");
            //event.getItem().setAmount(event.getItem().getAmount()+1);
        }

        if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            Block block = event.getClickedBlock();
            Location location = block.getLocation();
            double x = location.getX();
            double y = location.getY();
            double z = location.getZ();
            //event.getPlayer().sendMessage("你所点击的方块位于 x: " + x + "y：" + y + "z: " + z);
            if ((x > 60 && x < 153) && (y > 100 && y < 122) && (z > 578 && z < 689)) {
                // if(event.getAction() == Action.LEFT_CLICK_BLOCK ||event.getAction() == Action.RIGHT_CLICK_BLOCK)
                if (event.getPlayer().isOp()) {
                    //event.getPlayer().sendMessage("您是OP，可以进行方块操作！");
                    /*if (block.getType() == Material.CHEST) {
                        BlockState state = block.getState();
                        //state.setType(Material.APPLE);
                        //state.update();
                        Chest chest = (Chest) state;

                        Inventory inventory = chest.getBlockInventory();
                        ItemStack itemstack = new ItemStack(Material.APPLE);
                        ItemMeta itemMeta = itemstack.getItemMeta();
                        itemMeta.setDisplayName("§e§l超级大苹果");
                        itemstack.setItemMeta(itemMeta);
                        inventory.addItem(itemstack);
                        state.setType(Material.APPLE);
                        state.update();
                    }*/
                } else {
                    event.getPlayer().sendMessage("您不是OP，无法在狩猎场内操作方块！如有需要，请联系管理员！");
                    event.setCancelled(true);
                }
            }
        }
    }









}
